package com.fahai.web.service.impl;

import com.fahai.common.PageVo;
import com.fahai.web.dao.ClientManageDao;
import com.fahai.web.dao.UserInfoDao;
import com.fahai.web.entity.CannelInfo;
import com.fahai.web.entity.MpayflowVO;
import com.fahai.web.entity.QbyeVO;
import com.fahai.web.entity.UserInfo;
import com.fahai.web.service.UserInfoService;
import com.fahai.web.vo.CallFlownoVo;
import com.fahai.web.vo.ClientManageConditionVo;
import com.fahai.web.vo.UserInfoListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理业务层接口
 */
@Service
@Transactional(rollbackFor = DataAccessException.class)
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoDao dao_userInfo;
    @Autowired
    private ClientManageDao dao_client;


    private Map<String, Object> getQueryMap(ClientManageConditionVo userInfo, PageVo pageVo) {


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userid", userInfo.getUserid());
        if (userInfo.getUname() == null || userInfo.getUname().equals("")) {
            map.put("uname", null);
        } else {
            map.put("uname", userInfo.getUname());
        }
        //手机号
        if (userInfo.getUphone() == null || userInfo.getUphone().equals("")) {
            map.put("uphone", null);
        } else {
            map.put("uphone", userInfo.getUphone());
        }
        //电话地区
        if (userInfo.getUphoneArea() == null || userInfo.getUphoneArea().equals("")) {
            map.put("uphoneArea", null);
        } else {

            map.put("uphoneArea", "'" + userInfo.getUphoneArea().trim() + "'");
        }
        //专属律师
        if (userInfo.getLawyername() == null || userInfo.getLawyername().equals("")) {
            map.put("lawyername", null);
        } else {
            map.put("lawyername", userInfo.getLawyername());
        }

        //咨询次数大于
        if (userInfo.getGtAtimes() == null) {
            map.put("gtAtimes", null);
        } else {
            map.put("gtAtimes", userInfo.getGtAtimes());
        }
        //咨询次数小于
        if (userInfo.getLessAtimes() == null) {
            map.put("lessAtimes", null);
        } else {
            map.put("lessAtimes", userInfo.getLessAtimes());
        }

        //账号创建时间
        if (userInfo.getCreateTimestart() == null || userInfo.getCreateTimestart().equals("")) {
            map.put("createTimestart", null);
        } else {
            map.put("createTimestart", userInfo.getCreateTimestart());
        }
        if (userInfo.getCreateTimeend() == null || userInfo.getCreateTimeend().equals("")) {
            map.put("createTimeend", null);
        } else {
            map.put("createTimeend", userInfo.getCreateTimeend() + " 23:59:59");
        }
        //首次咨询时间
        if (userInfo.getFirstTimestart() == null || userInfo.getFirstTimestart().equals("")) {
            map.put("firstTimestart", null);
        } else {
            map.put("firstTimestart", userInfo.getFirstTimestart());
        }
        if (userInfo.getFirstTimeend() == null || userInfo.getFirstTimeend().equals("")) {
            map.put("firstTimeend", null);
        } else {
            map.put("firstTimeend", userInfo.getFirstTimeend() + " 23:59:59");
        }
        //末次咨询时间
        if (userInfo.getLastTimestart() == null || userInfo.getLastTimestart().equals("")) {
            map.put("lastTimestart", null);
        } else {
            map.put("lastTimestart", userInfo.getLastTimestart());
        }
        if (userInfo.getLastTimeend() == null || userInfo.getLastTimeend().equals("")) {
            map.put("lastTimeend", null);
        } else {
            map.put("lastTimeend", userInfo.getLastTimeend() + " 23:59:59");
        }
        //用户状态
        if (userInfo.getUstates() == null) {
            map.put("ustates", null);
        } else {
            map.put("ustates", userInfo.getUstates());
        }
        //排序字段
        if (userInfo.getPxzd() == null || userInfo.getPxzd().equals("")) {
            map.put("pxzd", "create_Time");//默认穿件日期
        } else {
            map.put("pxzd", userInfo.getPxzd().replace("T", "_t"));
        }
        //排序方向
        if (userInfo.getPxfx() == null || userInfo.getPxfx().equals("")) {
            map.put("pxfx", "desc");//
        } else {
            map.put("pxfx", userInfo.getPxfx());
        }


        return map;

    }

    @Override
    public UserInfoListVo getUserInfoListVoById(Integer userId) {
        ClientManageConditionVo conditionVo = new ClientManageConditionVo();
        conditionVo.setUserid(userId);
        List<UserInfoListVo> vo = dao_client.getClientList(conditionVo);
        if (null != vo && vo.size() > 0) {
            return vo.get(0);
        } else {
            return null;
        }
    }


    /**
     * 查看用户简明信息
     *
     * @param userId
     * @return
     */
    @Override
    public UserInfo getUserInfoById(Integer userId) {
        return dao_userInfo.selectByPrimaryKey(userId);
    }

    /**
     * 查看用户渠道
     *
     * @param userId
     * @return
     */
    @Override
    public CannelInfo findCannelInfo(Integer userId) {
        return null;
    }

    /**
     * 查看用户钱包余额
     *
     * @param userId
     * @return
     */
    @Override
    public List<QbyeVO> getWalletBalance(Integer userId) {
        return dao_userInfo.getWalletBalance(userId);
    }



    private  ClientManageConditionVo parseConditionVO(ClientManageConditionVo clientManageConditionVo){
        //归属地
        if (null != clientManageConditionVo.getUphoneArea() && !clientManageConditionVo.getUphoneArea().equals("")) {
            clientManageConditionVo.setUphoneArea(clientManageConditionVo.getUphoneArea().trim().replace("#", " "));
        }
        //创建时间
        if (clientManageConditionVo.getCreateTimeend() != null&&clientManageConditionVo.getCreateTimeend()!="") {
            clientManageConditionVo.setCreateTimeend(clientManageConditionVo.getCreateTimeend() + " 23:59:59");
        }
        //最后咨询时间
        if (clientManageConditionVo.getLastTimeend() != null&&clientManageConditionVo.getLastTimeend()!="") {
            clientManageConditionVo.setLastTimeend(clientManageConditionVo.getLastTimeend() + " 23:59:59");
        }
        //首次咨询时间
        if (clientManageConditionVo.getFirstTimeend() != null&&clientManageConditionVo.getFirstTimeend()!="") {
            clientManageConditionVo.setFirstTimeend(clientManageConditionVo.getFirstTimeend() + " 23:59:59");
        }

        return clientManageConditionVo;
    }

    /**
     * 用户查询
     *
     * @param clientManageConditionVo
     * @param pageVo
     * @return
     */
    @Override
    public PageVo getClientList(ClientManageConditionVo clientManageConditionVo, PageVo pageVo) {
         clientManageConditionVo=parseConditionVO(clientManageConditionVo);
        Map<String, Object> map = getQueryMap(clientManageConditionVo, pageVo);
        String pxzd=clientManageConditionVo.getPxzd();
        if(pxzd!=null&&!"".equals(pxzd)){
            if("createTime".equals(pxzd)){
                pxzd="create_time";//避免查询lawyer_info的createtime
            }
        }
        String orderByStr = pxzd + " " + clientManageConditionVo.getPxfx();
        Page<UserInfoListVo> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        PageHelper.orderBy(orderByStr);
        List<UserInfoListVo> listVos = dao_client.getClientList(clientManageConditionVo);
        for (UserInfoListVo vo : listVos
                ) {
            if(null!=vo.getUname()&&!"".equals(vo.getUname())) {
                try {
                    vo.setUname(URLDecoder.decode(vo.getUname(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        pageVo.setResult(listVos);
        return pageVo;
    }

    @Override
    public int getClientCount(ClientManageConditionVo clientManageConditionVo, PageVo pageVo) {
        clientManageConditionVo=parseConditionVO(clientManageConditionVo);
        return dao_client.getClientCount(clientManageConditionVo);
    }

    /**
     * 批量解冻用户
     *
     * @param idlist
     */
    @Override
    public void userunfrozeAccount(List<Integer> idlist) {
        dao_client.unfrozeAccount(idlist);
    }

    /**
     * 批量冻结
     *
     * @param idlist
     */
    @Override
    public void userfrozeAccount(List<Integer> idlist) {
        dao_client.frozeAccount(idlist);
    }

    /**
     * 编辑单个用户信息
     *
     * @param userInfo
     */
    @Override
    public void updateUserInfo(UserInfo userInfo) {
//        UserInfo oldVo = dao_userInfo.selectByPrimaryKey(userInfo.getUserid());
//        oldVo.setUstates(userInfo.getUstates());
        userInfo.setUname(null);
        dao_userInfo.updateByPrimaryKeySelective(userInfo);
    }

    /**
     * 编辑用户备注
     *
     * @param userInfo
     */
    @Override
    public void updateRemark(UserInfo userInfo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userid", userInfo.getUserid());
        map.put("remark", userInfo.getRemark());
        dao_userInfo.updateRemark(map);

    }

    @Override
    public List<Map<String, Object>> selectChartForHours(String registerTime) {

        if (registerTime != null && !"".equals(registerTime)) {


            if (registerTime.length() == 4) {

                return dao_userInfo.selectChartForMonths(registerTime);
            } else if (registerTime.length() == 7) {

                return dao_userInfo.selectChartForDays(registerTime);

            }

        }

        return dao_userInfo.selectChartForHours(registerTime);
    }

    @Override
    public void insertUserInfo(UserInfo userInfo) {
        dao_userInfo.insertUseGeneratedKeys(userInfo);
    }

    @Override
    public List<UserInfo> queryUserInfo(int proid, String uphone) {
        UserInfo userInfo = new UserInfo();
        userInfo.setProid(proid);
        userInfo.setUphone(uphone);
        return dao_userInfo.select(userInfo);
    }

    public int getEffectiveTimes(Integer uid) {
        return dao_client.getEffectiveTimes(uid);

    }

    @Override
    public PageVo findCallFlowList(UserInfo userInfo, PageVo pageVo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uid", userInfo.getUserid());
        map.put("initiator", 0);
        List<CallFlownoVo> callFlownoVoList = dao_client.selectRecordListByUserid(map);
        pageVo.setResult(callFlownoVoList);
        return pageVo;
    }

    /**
     * 查看M站消费记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<MpayflowVO> getMpayFlow(Integer userId) {

        return dao_userInfo.getMpayFlowVOs(userId);
    }


    /**
     * 查看M站充值记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<MpayflowVO> getPaymz(Integer userId) {
        return dao_userInfo.getPaymz(userId);
    }

    /**
     * 查看微信充值记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<MpayflowVO> getPaywx(Integer userId) {
        return dao_userInfo.getPaywx(userId);
    }


    /**
     * 查看公众号消费记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<MpayflowVO> getWxbody(Integer userId) {
        return dao_userInfo.getWxConsumption(userId);

    }
}
